package com.example.demo.controller.admin;

import com.example.demo.model.ArticleLabelModel;
import com.example.demo.model.Json;
import com.example.demo.services.ArticleLabelService;
import com.example.demo.services.ArticleService;
import com.example.demo.services.LabelService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/back/article_label")
public class ArticleLabelController {

    @Resource
    ArticleLabelService articleLabelService;

    @Resource
    ArticleService articleService;

    @Resource
    LabelService labelService;

    @RequestMapping("list")
    public ModelAndView list(
            Model model,
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize
    ){
        PageInfo pageInfo = articleLabelService.getArticleLabelListFromDb(page, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return new ModelAndView("admin/article_label/list");
    }

    @RequestMapping("add")
    public ModelAndView add(){
        return new ModelAndView("admin/article_label/add");
    }

    @RequestMapping("addPost")
    public Json addPost(
            @RequestBody Map<String, Object> params
            )
    {
        try{

            String labelIdString = (String)params.get("label_id");
            Long articleId       = Long.valueOf((String)params.get("article_id"));
            int onShelf          = Integer.parseInt(params.get("on_shelf").toString());
            int sort             = Integer.parseInt(params.get("sort").toString());
            String[] labelIds    = labelIdString.split(",");
            List<ArticleLabelModel> insertList = new ArrayList<>();
            for (String labelId: labelIds)
            {
                ArticleLabelModel articleLabelModel = new ArticleLabelModel();
                articleLabelModel.setArticle_id(articleId);
                articleLabelModel.setLabel_id(Long.valueOf(labelId));
                articleLabelModel.setOn_shelf(onShelf);
                articleLabelModel.setSort(sort);
                insertList.add(articleLabelModel);
            }

            boolean r = articleLabelService.batchInsert(insertList);

            if(r) {
                return new Json().success("新增成功");
            } else {
                return new Json().error("新增失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("edit")
    public ModelAndView edit(
            Model model,
            @RequestParam(name = "id", required = true) Long id
    ){
        ArticleLabelModel articleLabelInfo = articleLabelService.getArticleLabelInfo(id);
        model.addAttribute("info", articleLabelInfo);
        return new ModelAndView("admin/article_label/edit");
    }

    @RequestMapping("editPost")
    public Json editPost(
            @RequestBody ArticleLabelModel articleLabelModel
    ){
        try{
            boolean r = articleLabelService.edit(articleLabelModel);
            if(r) {
                return new Json().success("修改成功");
            } else {
                return new Json().error("修改失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

    @RequestMapping("del")
    public Json del(
            @RequestBody ArticleLabelModel articleLabelModel
    ){
        try{
            boolean r = articleLabelService.del(articleLabelModel.getId());
            if(r) {
                return new Json().success("修改成功");
            } else {
                return new Json().error("修改失败");
            }
        }catch (Exception e)
        {
            return new Json().error(e.getMessage());
        }
    }

}
